<?php
/**
 * Created by PhpStorm.
 * User: CY
 * Date: 2018/3/15
 * Time: 17:03
 */

namespace app\api\model;


class Category extends BaseModel
{
    /**
     * 关联商品表
     * @return \think\model\relation\HasMany
     */
    public function products(){
        return $this->hasMany('Product','category_id','id');
    }

    /**
     * 关联图片表
     * @return \think\model\relation\BelongsTo
     */
    public function img(){
        return $this->belongsTo('Image','topic_img_id','id');
    }

    /**
     * 获取指定分类集合和其中的商品
     * @param $ids
     * @return false|\PDOStatement|string|\think\Collection
     */
    public static function getCategories($ids){
        $categories = self::with('products')->with('products.imgs')->select($ids);
        return $categories;
    }

    /**
     * 获得具体分类和旗下商品
     * @param $id
     * @return array|false|\PDOStatement|string|\think\Model
     */
    public static function getCategory($id){
        $category = self::with('products')->with('products.imgs')->find($id);
        return $category;
    }
}